package com.hanter.rad.widget.cupertino.utils;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;

import com.hanter.pickerview.utils.R;
import com.hanter.rad.widget.cupertino.PickerViewDataSource;
import com.hanter.rad.widget.cupertino.PickerViewDelegate;

import java.util.Calendar;

public class DatePickerFragment extends BasePickerFragment {

    private final int startYear = 1900;
    private final int endYear = 2200;
    private Calendar calendar;

    @Nullable
    protected OnDatePickerClickListener listener;

    public DatePickerFragment() {
        this.calendar = Calendar.getInstance();
        this.calendar.set(Calendar.HOUR_OF_DAY, 12);
        this.calendar.set(Calendar.MINUTE, 0);
        this.calendar.set(Calendar.SECOND, 0);
        this.calendar.set(Calendar.MILLISECOND, 0);
        Log.d(TAG, calendar.getTime().toLocaleString());
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        if (context instanceof OnDatePickerClickListener) {
            listener = (OnDatePickerClickListener) context;
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnCancel) {
            dismiss();

            if (listener != null) {
                listener.onDatePickerCancel(getTag());
            }
        } else if (v.getId() == R.id.btnSubmit) {
            dismiss();

            if (listener != null) {
                listener.onDatePickerSelected(getTag(), calendar.getTime());
            }
        } else if (v.getId() == R.id.picker_root_container) {
            dismiss();
        }
    }

    @Nullable
    public OnDatePickerClickListener getListener() {
        return listener;
    }

    public void setListener(@Nullable OnDatePickerClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 添加 PickerProvider

        provider = new PickerProvider() {

            final int initPosition1 = calendar.get(Calendar.YEAR) - startYear;
            final int initPosition2 = calendar.get(Calendar.MONTH);
            final int initPosition3 = calendar.get(Calendar.DAY_OF_MONTH) - 1;

            @Override
            public PickerViewDataSource getDataSource(String tag) {
                return new PickerViewDataSource() {
                    @Override
                    public int getNumberOfComponentsInPickerView() {
                        return 3;
                    }

                    @Override
                    public int getNumberOfRowsInComponent(int component) {

                        switch (component) {
                            case 0:
                                return endYear - startYear + 1;

                            case 1:
                                return calendar.getActualMaximum(Calendar.MONTH) + 1;

                            case 2:
                                return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);


                            default:
                                return 0;
                        }
                    }
                };
            }

            @Override
            public PickerViewDelegate getDelegate(String tag) {
                return new PickerViewDelegate() {
                    @Override
                    public int getWidthForComponent(int component) {
                        return 0;
                    }

                    @Override
                    public int getRowHeightForComponent(int component) {
                        return 0;
                    }

                    @Override
                    public String getTitleForRow(int row, int component) {
                        if (component == 0) {
                            return String.valueOf(startYear + row);
                        } else if (component == 1) {
                            return String.valueOf(row + 1);

                        } else if (component == 2) {
                            return String.valueOf(row + 1);
                        }
                        return null;
                    }

                    @Override
                    public void didSelectRow(int row, int component) {
                        Log.d(TAG, "didSelectRow: row-" + row + ", component-" + component);

                        if (component == 0) {
                            calendar.add(Calendar.YEAR, row + startYear - calendar.get(Calendar.YEAR));
                            pvOptions.reloadComponent(1);
                            pvOptions.reloadComponent(2);
                        } else if (component == 1) {
                            calendar.add(Calendar.MONTH, row - calendar.get(Calendar.MONTH));
                            pvOptions.reloadComponent(2);
                        } else {
                            calendar.add(Calendar.DAY_OF_MONTH, row + 1 - calendar.get(Calendar.DAY_OF_MONTH));
                        }

                        Log.d(TAG, calendar.getTime().toLocaleString());
                    }

                    @Override
                    public int getInitPositionInComponent(int component) {
                        switch (component) {
                            case 0:
                                return initPosition1;

                            case 1:
                                return initPosition2;

                            case 2:
                                return initPosition3;
                        }
                        return 0;
                    }
                };
            }
        };

        pvOptions.setDataSource(provider.getDataSource(getTag()));
        pvOptions.setPickerDelegate(provider.getDelegate(getTag()));
    }
}
